//
//  SceneryListViewController.m
//  Travelmemory
//
//  Created by lanou3g on 16/3/31.
//  Copyright © 2016年 ABCD. All rights reserved.
//

#import "SceneryListViewController.h"
#import "DestinationCollectionViewCell.h"
#import "SceneryListModel.h"
#import "TravePictureViewController.h"
#import "TravelHelper.h"

#import "MJPhoto.h"
#import "MJPhotoBrowser.h"
#import "UIImageView+WebCache.h"
@interface SceneryListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *SceneryListCollectionView;

@property (nonatomic, strong) NSString *string;

@property (nonatomic, strong) NSMutableArray *ScenryListarrray;

// 存放图片数组
@property (nonatomic, strong) NSMutableArray *allPictureArray;

@end

static NSInteger page = 0;
@implementation SceneryListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ScenryListarrray = [NSMutableArray array];
    
    self.allPictureArray = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 标题名字
    self.navigationItem.title = [NSString stringWithFormat:@"%@图片",self.sceneryName];
    
    // navigation背景
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"daohang"] forBarMetrics:UIBarMetricsDefault];
    
    // 下拉刷新是要用这个来拼接字符串
    self.string = @"&start=";
    
    // 创建collectionView
    [self creatCollectionView];
    
    // 网络请求
    [self getData:page];
    
    // 注册
    [self.SceneryListCollectionView registerNib:[UINib nibWithNibName:@"DestinationCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Destinationcell"];
    
    // 上拉刷新下拉加载
//    [self setUpData];
    
    // 隐藏tabBar
    [self hideTabBar];
    
}
#pragma mark 隐藏hideTabBar
- (void)hideTabBar{
    
    if (self.tabBarController.tabBar.hidden == YES) {
        return;
    }
    UIView *contentView;
    
    if ([[self.tabBarController.view.subviews objectAtIndex:1] isKindOfClass:[UITabBar class]]) {
        
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
        
        contentView.frame = CGRectMake(contentView.bounds.origin.x,  contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
        self.tabBarController.tabBar.hidden = YES;
    }
    
}

#pragma mark 创建collectionView
- (void)creatCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置最小行间距
    layout.minimumLineSpacing = 4;
    
    // 设置最小列间距
    layout.minimumInteritemSpacing = 4;
    
    // 设置item大小
    layout.itemSize = CGSizeMake(self.view.frame.size.width / 3 - 6, self.view.frame.size.height * 0.17);
    
    // 设置section内边距
    layout.sectionInset = UIEdgeInsetsMake(5,5,5,5);
    
    self.SceneryListCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    
    // 设置背景颜色
    self.SceneryListCollectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.SceneryListCollectionView];
    
    self.SceneryListCollectionView.delegate = self;
    self.SceneryListCollectionView.dataSource = self;
    
}
#pragma mark MJfresh
//- (void)setUpData{
//    // 下拉
//    self.SceneryListCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upChangeData)];
//    
//    // 上拉
//    self.SceneryListCollectionView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downChageData)];
//}
//// 下拉
//- (void)upChangeData{
//    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        
//        [self.ScenryListarrray removeAllObjects];
//        
//        [self getData:0];
//        
//    });
//    
//}

//// 上拉
//- (void)downChageData{
//    
//    page += 18;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        
//        [self getData:page];
//        
//    });
//    
//}

# pragma mark 网络请求
- (void)getData:(NSInteger)sender{
    
    NSString *getstr = [NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18%@%ld&sign=5302361023ac5b508d6ec1deeaa07aff",self.sceneryType,self.scenerys,self.string,(long)sender];
    
    [[TravelHelper defaultTravelHelper] get:getstr params:nil success:^(id responseObj) {
        
        for (NSDictionary *dic in responseObj[@"items"]) {
            
            SceneryListModel *model = [SceneryListModel new];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.ScenryListarrray addObject:model];
            
            [self.allPictureArray addObject:model.photo];
            
        }
        
//        [self.SceneryListCollectionView.header endRefreshing];
//        
//        [self.SceneryListCollectionView.footer endRefreshing];
        
        [self.SceneryListCollectionView reloadData];
        
        
    } failure:^(NSError *error) {
        
//        [self.SceneryListCollectionView.header endRefreshing];
//        
//        [self.SceneryListCollectionView.footer endRefreshing];
        
//        UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"系统提示" message:@"网络链接失败,请查看网络连接" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
//        
//        [view show];
    }];
    
}

# pragma mark -- delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.ScenryListarrray.count;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;

}

// cell显示
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    // 重用cell
    DestinationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Destinationcell" forIndexPath:indexPath];
    SceneryListModel *model = self.ScenryListarrray[indexPath.row];
    
    [cell.destinationImage sd_setImageWithURL:[NSURL URLWithString:model.photo_s] placeholderImage:[UIImage imageNamed:@"11"]];
    
    cell.destinationLabel.text = nil;
    
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSMutableArray *phtotsArray = [NSMutableArray array];
    
    for (int i = 0; i < self.allPictureArray.count; i++) {
        
        MJPhoto *photo = [MJPhoto new];
        
        // 准备图片路径
        photo.url = [NSURL URLWithString:self.allPictureArray[i]];
        
        [phtotsArray addObject:photo];
        
    }
    
    // 显示相册
    MJPhotoBrowser *browser = [MJPhotoBrowser new];
    
    browser.currentPhotoIndex = indexPath.row;
    
    browser.photos = phtotsArray;
    
    [browser show];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
